/**
 *
 */
package com.ipudong.widget.textview;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

import com.ipudong.widget.R;

public class CustomFontTextView extends TextView {

	/**
	 * @param context context
	 * @param attrs attrs
	 */
	public CustomFontTextView (Context context, AttributeSet attrs) {
		super (context, attrs);
		init (context, attrs);
	}

	/**
	 * @param context context
	 */
	public CustomFontTextView (Context context) {
		super (context);
	}

	private void init (Context context, AttributeSet attrs) {
		TypedArray values = context.obtainStyledAttributes (attrs, R.styleable.CustomFontTextView);
		int font = values.getInt (R.styleable.CustomFontTextView_font, -1);
		values.recycle ();
		if (font != -1)
			setTypeface (creatTypeFace (context, font));
	}

	private Typeface creatTypeFace (Context context, int value) {
		Typeface font = null;

		if (!isInEditMode ()) {
			AssetManager asset = context.getAssets ();
			switch (value) {
				case 0:
					font = Typeface.createFromAsset (asset, "fonts/univers_67_condensed_bold.ttf");
					break;
				case 1:
					font = Typeface.createFromAsset (asset, "fonts/univers_condensed_bold_italic.ttf");
					break;
				case 2:
					font = Typeface.createFromAsset (asset, "fonts/univers_condensed_bold.ttf");
					break;
				case 3:
					font = Typeface.createFromAsset (asset, "fonts/univers_light_condensed_italic.ttf");
					break;
				case 4:
					font = Typeface.createFromAsset (asset, "fonts/univers_light_condensed.ttf");
					break;
				default:
					break;
			}
		} else {
			switch (value) {
				case 0:
					font = Typeface.defaultFromStyle (Typeface.BOLD);
					break;
				case 1:
					font = Typeface.defaultFromStyle (Typeface.BOLD | Typeface.ITALIC);
					break;
				case 2:
					font = Typeface.defaultFromStyle (Typeface.BOLD);
					break;
				case 3:
					font = Typeface.defaultFromStyle (Typeface.ITALIC);
					break;
				case 4:
					font = Typeface.defaultFromStyle (Typeface.NORMAL);
					break;
			}
		}

		return font;
	}

}
